<?php
/**
 * Registry-Klasse zur globalen Speicherung
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


Final Class Yapary_MVC_SimpleMVC_Registry
{
	/** @var array $arrData	:	Feld mit allen gespeicherten Daten */
	private $arrData			= array();
	static private $instance	= NULL;
	private $initialized		= false;
	public $arrRouteParam		= array();


    /**
     * @return Yapary_MVC_SimpleMVC_RegistryData
     * */
	public static function getInstance () {
    	if(self::$instance === NULL) {
			self::$instance = new Yapary_MVC_SimpleMVC_Registry();
		}

		return self::$instance;
    }

	public function initialize (
    		Yapary_Header_Header &$_oHeader,
    		Yapary_Header_Metatag &$_oMetatag,
    		Yapary_Header_Http &$_oHttp,
    		Yapary_Header_OpenGraph &$_oOpenGraph,
    		Yapary_Route_XmlRoute &$_oRoute,
    		Yapary_Db_Sql_Connector &$_oSqlConnector,
    		Yapary_Parameter_Parameter &$_oParameter,
			array $_arrRouteParameter
	) {
		if ($this->initialized === true) {
    		throw new Exception('Yapary_MVC_SimpleMVC_Registry already initialized');
    		return false;
		}

		$this->set('oHeader', $_oHeader);
		$this->set('oMetatag', $_oMetatag);
    	$this->set('oHttp', $_oHttp);
    	$this->set('oOpenGraph', $_oOpenGraph);
    	$this->set('oRoute', $_oRoute);
    	$this->set('oSql', $_oSqlConnector);
    	$this->set('oParameter', $_oParameter);
    	$this->set('oValue', new Yapary_MVC_SimpleMVC_Value());
    	$this->arrRouteParam	= $_arrRouteParameter;

    	$this->initialized	= true;
	}


	/**
	 * Gibt aus, ob die Initialisierung durchgefuert wurde
	 * */
	public function isInitialized () {
		return $this->initialized;
	}


    /**
     * Erstellt eine Variable
     * @param string $name	: Name der zu erstellenden Variable
     * @param mixed $value	: Wert zum Namen
     * @return boolean
     * */
	public function set ($_name, &$_object) {
    	$this->arrData[$_name] = &$_object;
    }

    /**
     * Erstellt eine Variable
     * @param string $name	: Name der zu erstellenden Variable
     * @param mixed $value	: Wert zum Namen
     * @return boolean
     * */
    public function get ($_name) {
    	if (!isset($this->arrData[$_name])) {
    		throw new Exception('Object '.$_name.' not found');
    	}

		return $this->arrData[$_name];
    }


    /**
     * @return Yapary_MVC_SimpleMVC_Value
     * */
    public function getValue ()
    {
    	return $this->arrData['oValue'];
    }
    /**
     * @return Yapary_Header_Header
     * */
    public function getHeader ()
    {
    	return $this->arrData['oHeader'];
    }
    /**
     * @return Yapary_Header_Metatag
     * */
    public function getMetatag ()
    {
    	return $this->arrData['oMetatag'];
    }
    /**
     * @return Yapary_Header_Http
     * */
    public function getHttp ()
    {
    	return $this->arrData['oHttp'];
    }
    /**
     * @return Yapary_Header_OpenGraph
     * */
    public function getOpenGraph ()
    {
    	return $this->arrData['oOpenGraph'];
    }
    /**
     * @return Yapary_Route_XmlRoute
     * */
    public function getRoute ()
    {
    	return $this->arrData['oRoute'];
    }
    /**
     * @return Yapary_Parameter_Parameter
     * */
    public function getParameter ()
    {
    	return $this->arrData['oParameter'];
    }
    /**
     * @return Yapary_Db_Sql_Connector
     * */
    public function getSqlConnector ()
    {
    	return $this->arrData['oSql'];
    }


    /**
     * Prueft, ob ein Datensatz bereits existiert
     * @param string $name	: Name der zu erstellenden Variable
     * @return bool
     * */
    public function exists ($_name)
    {
    	if (isset($this->arrData[$_name])) {
    		return true;
    	}

    	return false;
    }


    /**
     * Zaehlt die Anzahl an Eintraege
     * @return int
     * */
    public function getCount ()
    {
    	return (int) count($this->arrData);
    }


    /**
     * Gibt alle Eintraege in foglendem Format aus
     * array('name1' => Object1, 'name2' => Object2, ...)
     * @return array
     * */
    public function getList ()
    {
    	return (array) $this->arrData;
    }



}


?>